package com.wzd.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzd.commonutils.JwtUtils;
import com.wzd.commonutils.R;
import com.wzd.eduservice.entity.EduCourse;
import com.wzd.eduservice.entity.EduTeacher;
import com.wzd.eduservice.entity.Live;
import com.wzd.eduservice.entity.frontvo.CourseQueryVo;
import com.wzd.eduservice.service.EduCourseService;
import com.wzd.eduservice.service.EduTeacherService;
import com.wzd.eduservice.service.LiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.internal.constraintvalidators.bv.time.past.PastValidatorForYear;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.temporal.ValueRange;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wzd
 * @since 2021-04-25
 */
@RestController
@Api(tags = "课程直播")
@RequestMapping("/eduservice/live")
public class LiveController {
    @Autowired
    private LiveService liveService;

    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private EduCourseService courseService;

    @PostMapping("addLive")
    @ApiOperation("添加直播")
    public R addLive(@RequestBody Live live) {
        boolean save = liveService.save(live);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    @GetMapping("getLiveByCourseId/{id}")
    @ApiOperation("获取直播根据课程id")
    public R getLiveByCourseId(@PathVariable("id") String id) {
        QueryWrapper<Live> liveQueryWrapper = new QueryWrapper<>();
        liveQueryWrapper.eq("cource_id",id);
        Live one = liveService.getOne(liveQueryWrapper);
        return R.ok().data("lives", one);
    }
    @GetMapping("getLives")
    @ApiOperation("获取直播")
    public R getLives(HttpServletRequest request) {
        //远程调用，判断课程是否被购买
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberIdByJwtToken)) {
            return R.error().code(28004).message("请登录");
        }
        // 根据用户id查询教师id
        QueryWrapper<EduTeacher> eduTeacherQueryWrapper = new QueryWrapper<>();
        eduTeacherQueryWrapper.eq("user_id", memberIdByJwtToken);
        EduTeacher one = teacherService.getOne(eduTeacherQueryWrapper);
        List<Live> list = new ArrayList<>();
        if (one != null) {
            // 查询直播
            QueryWrapper<Live> liveQueryWrapper = new QueryWrapper<>();
            liveQueryWrapper.eq("teacher_id", one.getId());
            liveQueryWrapper.orderByDesc("gmt_create");
            list = liveService.list(liveQueryWrapper);
        }
        return R.ok().data("lives", list);
    }

    @GetMapping("startOnLine/{id}")
    @ApiOperation("开始直播")
    public R startOnLine(@PathVariable String id) {
        boolean save = liveService.startOnLine(id);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("updataPublic/{id}")
    @ApiOperation("是否为公开课")
    public R updataPublic(@PathVariable String id) {
        boolean save = liveService.updataPublic(id);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("isPublicByCourseId/{courseId}")
    @ApiOperation("是否为公开课")
    public boolean isPublicByCourseId(@PathVariable String courseId) {
        return liveService.isPublicByCourseId(courseId);
    }

    @ApiOperation(value = "分页直播列表")
    @PostMapping(value = "liveFrontPageList/{page}/{limit}")
    public R liveFrontPageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQueryVo courseQuery, HttpServletRequest request) {
        //远程调用，判断课程是否被购买
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberIdByJwtToken)) {
            return R.error().code(28004).message("请登录");
        }
        Page<EduCourse> pageParam = new Page<EduCourse>(page, limit);
        Map<String, Object> map = liveService.liveFrontPageList(pageParam, courseQuery, memberIdByJwtToken);
        return R.ok().data(map);
    }

}

